﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Metadata.Entity;
using System.Data;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaDataVersionDao : DaoService<MetaDataVersion>
    {
       // TMetaDataContentDao contentDao;

        public MetaDataVersionDao(string db = "TFAS") : base()
        {
            this.DBName = db;
            //contentDao = new TMetaDataContentDao();
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_Version";
            }
        }

        private List<string> ExceptColumnNameList = new List<string>() { "Content" };
        public override List<string> ExceptColumnName
        {
            get
            {
                return ExceptColumnNameList;
            }
        }

        public int UpdateMetaDataVersion(MetaDataVersion dataVersion)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();            
            paramsList.Add(this.DB.MakeInParam("@ContentJSON", TDbDataType.VarChar, dataVersion.ContentJSON));
            return this.DB.ExecSqlStatement(string.Format("Update {0} Set ContentJSON=@ContentJSON,ContentID='T' Where ID='{1}'", 
                TableName,dataVersion.ID), paramsList.ToArray());
        }

        public MetaDataVersion GetMetaDataVersion(string versionID)
        {
            var version = new MetaDataVersion();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = string.Format("ID=@VersionID");
            paramsList.Add(this.DB.MakeInParam("@VersionID", TDbDataType.VarChar, versionID));
            string sql = "select ID, MetadataID, MetadataCode, Version, VersionMask, ContentID, Creator, CreateTime, LastModifier, LastModifyTime, ContentJSON, Terms, ProjectBranchID from Meta_Version";

            ExceptColumnNameList.Add("MetaDataJSON");
            var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());
            
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                version = base.ConvertToObject(this.Columns, ds.Tables[0].Rows[0]);                    
            }
            return version;
        }
        public MetaDataVersion GetMetaDataTerms(string versionID)
        {
            var version = new MetaDataVersion();
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            string sqlCondition = string.Format("ID=@VersionID");
            paramsList.Add(this.DB.MakeInParam("@VersionID", TDbDataType.VarChar, versionID));
            string sql = "select Terms from Meta_Version";

            ExceptColumnNameList.Add("MetaDataJSON");
            var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                version.Terms = Convert.ToString(ds.Tables[0].Rows[0]["Terms"]);
            }
            return version;
        }

        public MetaDataVersion GetMetaDataVersion(string metaDataId, string version)
        {
            var filter = string.Format("ID='{0}' and Version='{1}'", metaDataId, version);
            var versions = this.GetObjectsByFilter(filter);
            if (versions != null)
            {
                var versionTemp = versions.FirstOrDefault() as MetaDataVersion;
                if (versionTemp != null)
                {
                    //var content = contentDao.GetMetaDataContentByID(versionTemp.ContentID);
                    //versionTemp.Content = content;
                }
                return versionTemp;
            }

            return null;
        }      
        public void DeleteByMetaDataId(string metaDataId, string projectBranchId)
        {
            var sql = string.Format("delete from {0} where ID = '{1}' AND ProjectBranchID='{2}'", TableName, metaDataId, projectBranchId);
            DB.ExecSqlStatement(sql);
        }

        public decimal GetMaxVersion(string metaDataId) {
            try
            {
                if (string.IsNullOrEmpty(metaDataId))
                {
                    throw new ArgumentNullException("元数据标识为空");
                }
                if (this.DB == null)
                {
                    throw new ArgumentNullException("this.DB为空");
                }
                decimal versionMask = 1000000000000;
                List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
                string sqlCondition = string.Format("MetaDataID=@MetaDataID");
                paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, metaDataId));
                string sql = "select MAX(VersionMask) VersionMask from Meta_Version";
                var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());
                if (ds.Tables == null)
                {
                    throw new ArgumentNullException("Tables为空1");
                }
                if (ds.Tables[0].Rows == null)
                {
                    throw new ArgumentNullException("Tables[0].Rows为空1");
                }
                if (ds.Tables[0].Rows[0] == null)
                {
                    throw new ArgumentNullException("Tables[0].Rows[0]为空1");
                }
                if (ds.Tables[0].Rows[0]["VersionMask"] == null)
                {
                    throw new ArgumentNullException("Tables[0].Rows[0]VersionMask为空1");
                }

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && DBNull.Value != ds.Tables[0].Rows[0]["VersionMask"])
                {
                    versionMask = Convert.ToDecimal(ds.Tables[0].Rows[0]["VersionMask"]);
                }
                else
                {
#if NET451
                ds = this.DB.ExecuteDataSet("select 1 from Meta_BasicInfo WHERE ID=@MetaDataID", paramsList.ToArray());
                if (ds.Tables == null)
                {
                    throw new ArgumentNullException("Tables为空2");
                }
                if (ds.Tables[0].Rows == null)
                {
                    throw new ArgumentNullException("Tables[0].Rows为空2");
                }
                if (ds.Tables[0].Rows[0] == null)
                {
                    throw new ArgumentNullException("Tables[0].Rows[0]为空2");
                }
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    versionMask = 9000000000000;
                }
#endif
                }
                return versionMask;
            }
            catch (Exception ex) {
                LogUtil.Log(ex.ToString(), "FASVersion");
                return 9000000000000;
            }
        }
        public string GetMetaDataVersionByID(string id)
        {
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@ID", TDbDataType.VarChar, id));
            var ds = this.DB.ExecuteDataSet("select Version from Meta_Version where ID=@ID", paramsList.ToArray());
            string version = "";
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                version = Convert.ToString(ds.Tables[0].Rows[0]["Version"]);
            }
            return version;
        }
    }
}
